Rein.NewFolderPage = function() {
    
    this.init = function() {
        this.lbxSelParent = new Rein.TreeListbox('#treeSelectParent');
        this.$txtParentId = $('#txtHiddenParentId');
        
        $(this.lbxSelParent).on('select', $.proxy(this.handleFolderSelect, this));
        
        this.loadFolders();
    }
    
    this.loadFolders = function() {
        $.ajax({
            url: 'GetFolderTree',
            type: 'GET',
            success: $.proxy(function(jsonTree) {
                this.displayFolder(jsonTree, 0);
                this.lbxSelParent.select($.cookie('currentFolderId'));
            }, this),
            dataType: 'json'
        });
    }
    
    this.displayFolder = function(jsonFolder, level) {
        this.lbxSelParent.addItem(jsonFolder.id, jsonFolder.name, level);
        $.each(jsonFolder.childs, $.proxy(function(i, child){
            this.displayFolder(child, level + 1);
        }, this));
    }
    
    this.handleFolderSelect = function() {
        this.$txtParentId.val(this.lbxSelParent.getSelectedValue());
    }
    
    this.init();
}


$(document).on('ready', function() {
    new Rein.NewFolderPage();
});